import time
import datetime
import win32com.client
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec

if __name__ == '__main__':
    speaker = win32com.client.Dispatch("SAPI.SpVoice")
    times = '2023-12-10 18:49:10'  # 秒杀开始时间
    driver = webdriver.Edge()
    driver.get("https://www.taobao.com")
    time.sleep(10)

    driver.find_element(By.XPATH,'//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]').click() #点击登录
    driver.maximize_window()

    driver.find_element(By.XPATH, '//*[@id="login"]/div[1]/i').click()
    time.sleep(10)

    driver.find_element(By.XPATH,'//*[@id="J_MiniCart"]/div[1]/a').click()

    driver.find_element(By.XPATH,'//*[@id="J_SelectAll1"]').click()
    #开始一直等，直到抢购时间到，程序就会点击自动结算
    while True:
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(now)

        if now > times :
            driver.find_element(By.XPATH,'//*[@id="J_Go"]/span').click()
            break
    wait = WebDriverWait(driver,20)
    element = wait.until(ec.presence_of_element_located(By.CLASS_NAME,"go-btn"))
    element.click()

    #使用speaker模块 提示信息，因暂时没安装所以没写
    speaker.Speak(f"This is a flash sale testing program")
    time.sleep(100000)




